package cmc.asiainfo.com.baidumap.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

/**
 * @Author xihaitao
 * @Date 2016/12/9
 * @Function:
 *      Bitmap图片处理工具类
 */

public class BitmapUtil {
    /**
     * 方法名：getBitmapFromSDCard
     * 功能：根据图片路径获取手机SD卡内的图片并以Bitmap格式返回
     * @param path
     * @return
     */
    public static Bitmap getBitmapFromSDCard(String path) {
        FileInputStream fis  =null;
        try {
            fis =  new FileInputStream(path);
            if(fis!=null){
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 2;
                return BitmapFactory.decodeStream(fis, null, options);
            }
        } catch (Exception e) {

        }
        return null;
    }

    /**
     * 方法名：zoomBitmap
     * 功能：等比例缩放，按宽度（或者高度）的缩放比进行缩放
     * 说明：第二和第三个参数分别是指定的宽度和高度
     * @param bitmap
     * @param width
     * @param height
     * @return
     */
    public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidth = ((float) width / w);
        float scaleHeight = ((float) height / h);
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
        return newbmp;
    }

    /**
     * 方法名：saveBitmapToSDCard
     * 功能：将Bitmap图片保存到指定路径
     * @param bitmap
     * @param path
     */
    public static void saveBitmapToSDCard(Bitmap bitmap,String path)  {
        File file=new File(path);
        if (file.exists()){
            file.delete();
        }
        FileOutputStream outputStream=null;
        try{
            outputStream=new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
            outputStream.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("----------save success-------------------");
    }

    /**
     * 方法名：bitmapZoomByHeight
     * 功能：按照高度的百分比压缩
     * @param srcBitmap
     * @param newHeight
     * @return
     */
    public static Bitmap bitmapZoomByHeight(Bitmap srcBitmap,float newHeight)
    {
        int srcWidth = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();

        float scaleHeight = ((float) newHeight) / srcHeight;
        float scaleWidth = scaleHeight;

        return bitmapZoomByScale(srcBitmap, scaleWidth, scaleHeight);

    }

    /**
     * 方法名：bitmapZoomByWidth
     * 功能：按照宽度百分比压缩
     * @param drawable
     * @param newWidth
     * @return
     */
    public static Bitmap bitmapZoomByWidth(Drawable drawable, float newWidth)
    {
        Bitmap srcBitmap=drawableToBitmap(drawable);
        int srcWidth = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();

        float scaleWidth = ((float) newWidth) / srcWidth;
        float scaleHeight = scaleWidth;

        return bitmapZoomByScale(srcBitmap, scaleWidth, scaleHeight);

    }

    /**
     * 方法名：bitmapZoomByScale
     * 功能：使用长宽缩放比缩放
     * 说明：第二和第三参数是指定的宽度缩放比和长度缩放比
     * @param srcBitmap
     * @param scaleWidth
     * @param scaleHeight
     * @return
     */
    public static Bitmap bitmapZoomByScale(Bitmap srcBitmap,float scaleWidth,float scaleHeight)
    {
        int srcWidth = srcBitmap.getWidth();
        int srcHeight = srcBitmap.getHeight();
        Matrix matrix = new Matrix();

        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizedBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcWidth,
                srcHeight, matrix, true);
        if(resizedBitmap != null)
        {
            srcBitmap=null;
            return resizedBitmap;
        } else
        {
            return srcBitmap;
        }
    }

    /**
     * 方法名：drawableToBitmap
     * 功能：Drawable转化为Bitmap
     * @param drawable
     * @return
     */
    public static Bitmap drawableToBitmap(Drawable drawable) {
        int width = drawable.getIntrinsicWidth();
        int height = drawable.getIntrinsicHeight();
        Bitmap bitmap = Bitmap.createBitmap(width, height, drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, width, height);
        drawable.draw(canvas);
        return bitmap;
    }
}
